﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Presenter.Users;

namespace SocialNetworkClient.Pages.User
{
    public partial class DiaryMessages : System.Web.UI.Page, IUserDiaryMessagesView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userid = Request.QueryString["id"];

            if (string.IsNullOrEmpty(userid))
            {
                if (Session["UserDiaryMessagesPresenter"] != null)
                {
                    var presenter = Session["UserDiaryMessagesPresenter"] as UserDiaryMessagesPresenter;
                    if (presenter != null)
                        presenter.Reload(this, (string)Session["Email"], (string)Session["Password"]);
                }
                else
                {
                    Session["UserDiaryMessagesPresenter"] = new UserDiaryMessagesPresenter(this, (string)Session["Email"], (string)Session["Password"]);
                }
            }
            else
            {
                if (Session["UserDiaryMessagesPresenter"] != null)
                {
                    var presenter = Session["UserDiaryMessagesPresenter"] as UserDiaryMessagesPresenter;
                    if (presenter != null)
                        presenter.Reload(this, Convert.ToInt32(userid));
                }
                else
                {
                    Session["UserDiaryMessagesPresenter"] = new UserDiaryMessagesPresenter(this, Convert.ToInt32(userid));
                }

                CreateDiv.Visible = (!string.IsNullOrEmpty((string)Session["UserId"]) && userid == (string)Session["UserId"]);
            }
        }

        public void CreateLbButtonClick(object sender, EventArgs e)
        {
            if (Session["Email"] != null && Session["Password"] != null)
            {
                if (EntityCreated != null)
                    EntityCreated((string)Session["Email"], (string)Session["Password"], TitleTb.Text, TextTb.Text);

                Response.Redirect(@"~/Pages/User/DiaryMessages.aspx");
            }
        }

        #region Implementation of IUserDiaryMessagesView

        public void GetMessages(List<List<string>> messages)
        {
            MessageTable.Rows.Clear();

            var headerRow = new TableRow();
            headerRow.Cells.Add(new TableCell { Text = "<b>Id</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>Title</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>Text</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>DateTime</b>" });
            MessageTable.Rows.Add(headerRow);

            foreach (var item in messages)
            {
                var row = new TableRow();

                var cell = new TableCell { Text = item[0] };
                row.Cells.Add(cell);

                cell = new TableCell { Text = item[1] };
                row.Cells.Add(cell);

                cell = new TableCell { Text = item[2] };
                row.Cells.Add(cell);

                cell = new TableCell { Text = item[3] };
                row.Cells.Add(cell);

                if (CreateDiv.Visible)
                {
                    var deleteCell = new TableCell();

                    var deleteButton = new LinkButton { Text = "Delete", Visible = false };
                    deleteButton.Click += delegate
                    {
                        if (EntityDeleted != null)
                            EntityDeleted((string)Session["Email"],
                                          (string)Session["Password"], Convert.ToInt32(item[0]));

                        Response.Redirect(@"~/Pages/User/DiaryMessages.aspx");
                    };

                    deleteCell.Controls.Add(deleteButton);
                    row.Cells.Add(deleteCell);
                }

                MessageTable.Rows.Add(row);
            }
        }

        public event Action<string, string, int> EntityDeleted;
        public event Action<string, string, string, string> EntityCreated;

        #endregion
    }
}